package com.shen.server;

import com.shen.pojo.MobileCard;
import com.shen.server.serive.NetSerice;
import com.shen.util.CardUtil;
import com.shen.util.Common;

/**
 * Author:shen
 * CreateDate:2020/6/11.
 * Function:网虫套餐
 */
public class NetPackage extends ServicePackage implements NetSerice {
    private int flow=1024;//上网流量

    @Override
    public String showInfo() {
        return super.showInfo();
    }

    CardUtil cardUtil=new CardUtil();

    @Override
    public int netPlay(int flow, MobileCard mobileCard) throws Exception {
        int temp=flow;
        //分为两种情况
        for (int i = 0; i <(flow*1024); i++) {
            //1.第一种套餐支持再消费1MB
            if(this.flow-mobileCard.getRealFlow()>=1){
                mobileCard.setRealFlow(mobileCard.getRealFlow()+1);
                cardUtil.updateCardMap(mobileCard);
            }else if(mobileCard.getMoney()>=0.1){//是否能支付0.1元
                //2.第二种套餐不支持再消费1GB，需要支付额外的费用 超出的上网流量0.1元/MB
                mobileCard.setRealFlow(mobileCard.getRealFlow()+1);
                //计算额外费用
                mobileCard.setMoney((mobileCard.getMoney()-0.1));
                //消费
                mobileCard.setConsumAmount(mobileCard.getConsumAmount()+0.1);
                cardUtil.updateCardMap(mobileCard);
            }else{
                temp=i;
                throw new Exception("本次已使用"+i+"MB,您的余额不足，请充值后再使用！\n");
            }
        }
        return temp;
    }

    public NetPackage() {
        this.price=68;
    }

    public NetPackage(int flow, double price) {
        this.flow = flow;
        this.price = price;
    }

    public int getFlow() {
        return flow;
    }

    public void setFlow(int flow) {
        this.flow = flow;
    }

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "网虫套餐：上网流量为"+ Common.dataFormat(this.flow*1.0/1024)+"GB/月";
    }
}
